package com.ITXu.reggie.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ITXu.reggie.DTO.DishDto;
import com.ITXu.reggie.commom.CustomException;
import com.ITXu.reggie.commom.R;
import com.ITXu.reggie.entity.Category;
import com.ITXu.reggie.entity.Dish;
import com.ITXu.reggie.entity.DishFlavor;
import com.ITXu.reggie.service.CategoryService;
import com.ITXu.reggie.service.DishFlavorService;
import com.ITXu.reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/dish")
public class DishController {
	
	@Autowired
	private DishService dishService;
	
	@Autowired
	private DishFlavorService dishFlavorService;
	
	@Autowired
	private CategoryService categoryService;
	
	@PostMapping
	public R<String> save(@RequestBody DishDto Dto) {
		log.info(Dto.toString());
		dishService.saveWithDishFlavor(Dto);
		return R.success("新增菜品成功");
	}
	
	@GetMapping("/page")
	public R<Page> page(int page, int pageSize, String name) {
		//构造分页构造器
		Page<Dish> pageInfo = new Page<>(page, pageSize);
		//构造泛型为DishDto的分页构造器
		Page<DishDto> DtoPageInfo = new Page<>(page, pageSize);
		//构造条件查询器
		LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		//注入查询条件
		queryWrapper.like(name != null, Dish::getName, name);
		queryWrapper.orderByDesc(Dish::getUpdateTime);
		//调用page方法,完善pageInfo属性
		dishService.page(pageInfo, queryWrapper);
		//把pageInfo中除records之外的属性赋值到DtoPageInfo(records是一个dish集合)
		BeanUtils.copyProperties(pageInfo, DtoPageInfo, "records");
		//把records中的dish对象通过map转换成有categoryName属性的dishdto对象,此时返回的records重新赋值到一个泛型为DishDto的集合 list 中
		List<Dish> records = pageInfo.getRecords();
		List<DishDto> list = null;
		list = records.stream().map((item)-> {
			DishDto dishDto = new DishDto();
			BeanUtils.copyProperties(item, dishDto);
			//通过categoryId获得对应的category对象，取得categoryName
			Long categoryId = item.getCategoryId();
			Category category = categoryService.getById(categoryId);
			if (category != null) {
				String categoryName = category.getName();
				dishDto.setCategoryName(categoryName);
			}
			return dishDto;
	    }).collect(Collectors.toList());
		//把list赋值给DtoPageInfo
		DtoPageInfo.setRecords(list);
		return R.success(DtoPageInfo);
	}
	
	/**
	 * 根据菜品id查询菜品信息和口味信息 
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public R<DishDto> getById(@PathVariable Long id) {
		DishDto dishDto = dishService.getFlavorsAndDishById(id); 
		return R.success(dishDto);
	}
	
	/**
	 * 根据dto修改dish和dish_flavor
	 * @param Dto
	 * @return
	 */
	@PutMapping
	public R<String> update(@RequestBody DishDto Dto) {
		dishService.upateWithFlavor(Dto);
		return R.success("修改菜品成功");
	}
	
	/**
	 * 根据ids更改status属性
	 * @param ids
	 * @return
	 */
	@PostMapping("/status/0/{ids}")
	public R<String> changeStatus(@PathVariable Long ids) {
		LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		return R.success("状态修改成功");
	}
	
	/**
	 * 根据ids删除dish
	 * @param ids
	 * @return
	 */
	@DeleteMapping
	public R<String> delete(Long ids) {
		log.info("删除分类，id为{}", ids);
		LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Dish::getId, ids);
		dishService.remove(queryWrapper);
		return R.success("菜品信息删除成功");
	}
	
	/**
	 * 根据条件查询对应的dish
	 * @param dish
	 * @return
	 */
	@GetMapping("/list")
	public R<List<DishDto>> list(Dish dish) {
		LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
		queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
		List<Dish> list = dishService.list(queryWrapper);
		for (Dish dishObject : list) {
			Integer status = null;
			status = dishObject.getStatus();
			if ( status.equals(0)) {
				dishObject = null;
			}
		};
		List<DishDto> DishDtos = new ArrayList<>();
		log.info(DishDtos.toString());
		DishDtos = list.stream().map((item) -> {
			DishDto dishDto = new DishDto();
			BeanUtils.copyProperties(item, dishDto);
			//获取dishflavors
			LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(DishFlavor::getDishId, item.getId());
			List<DishFlavor> flavors = null;
			flavors = dishFlavorService.list(lambdaQueryWrapper);
			dishDto.setFlavors(flavors);
			return dishDto;
		}).collect(Collectors.toList());
			return R.success(DishDtos);
	}
}
